package 栈;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

/**
 * @Author: biubiu0002
 * @Date: 2021-03-26 13:19
 **/

/**
 * leetcode栈题目：
 * 20,155,232,844,224,682,496.
 */

public class 合法括号 {

    public boolean isValid(String s) {
        Deque<String> stack = new ArrayDeque<>();
        stack.push(s.substring(0, 1));
        int i = 1;
        while (!stack.isEmpty()) {
            if (i == s.length())
                return false;

            String charStr = s.substring(i, i + 1);
            String lastElem = stack.peekLast();
            if (lastElem.equals("(") && charStr.equals(")")) {
                stack.pop();
            }
            if (lastElem.equals("[") && charStr.equals("[")) {
                stack.pop();
            }
            if (lastElem.equals("{") && charStr.equals("}")) {
                stack.pop();
            }
            if (charStr.equals("(") || charStr.equals("[") || charStr.equals("{"))
                stack.push(charStr);
            i++;


        }
        return true;

    }

    public static void main(String[] args) {
        合法括号 test = new 合法括号();
        System.out.println(test.isValid("()"));

    }
}
